package com.kfirszt.elearning.struts.actions;

import com.kfirszt.elearning.base.BaseAction;
import com.kfirszt.elearning.dictionary.QuestionType;
import com.kfirszt.elearning.persistence.model.Answer;
import com.kfirszt.elearning.persistence.model.Question;
import com.kfirszt.elearning.persistence.model.QuestionAttempt;
import com.kfirszt.elearning.persistence.model.QuizAttempt;
import com.kfirszt.elearning.utils.SessionAttr;
import com.kfirszt.elearning.utils.SessionUtil;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class TestAction extends BaseAction implements Preparable, ModelDriven<QuestionAttempt> {

	private static final long serialVersionUID = 1L;
	
	private QuestionAttempt question;

	public String submit() throws Exception {
		LOG.info("test-input action");
		System.out.println("test-input action");
		return SUCCESS;
	}
	
	@Override
	public String execute() throws Exception {
		return INPUT;
	}

	private Question build1Question() {
		Question question1 = new Question();
		question1.setContent("Ile jest 2 * 2");
		question1.setQuestionType(QuestionType.ONE_CHOICE);
		Answer answer1 = new Answer();
		answer1.setValue("3");
		question1.getAnswers().add(answer1);
		Answer answer2 = new Answer();
		answer2.setValue("4");
		question1.getAnswers().add(answer2);
		Answer answer3 = new Answer();
		answer3.setValue("5");
		question1.getAnswers().add(answer3);
		Answer answer4 = new Answer();
		answer4.setValue("6");
		question1.getAnswers().add(answer4);
		return question1;
	}
	
	private Question build2Question() {
		Question question1 = new Question();
		question1.setContent("Która liczba jest nieparzysta");
		question1.setQuestionType(QuestionType.MULTI_CHOICE);
		Answer answer1 = new Answer();
		answer1.setValue("3");
		question1.getAnswers().add(answer1);
		Answer answer2 = new Answer();
		answer2.setValue("4");
		question1.getAnswers().add(answer2);
		Answer answer3 = new Answer();
		answer3.setValue("5");
		question1.getAnswers().add(answer3);
		Answer answer4 = new Answer();
		answer4.setValue("9");
		question1.getAnswers().add(answer4);
		return question1;
	}

	public QuestionAttempt getQuestion() {
		return question;
	}

	public void setQuestion(QuestionAttempt question) {
		this.question = question;
	}

	@Override
	public void prepare() throws Exception {
		if(SessionUtil.getSessionAttribute(SessionAttr.QUIZ_ATTEMPT)==null){
			QuizAttempt attempt = new QuizAttempt();
			QuestionAttempt questionAttempt1 = new QuestionAttempt();
			Question question1 = build1Question();
			questionAttempt1.setQuestion(question1);
			
			QuestionAttempt questionAttempt2 = new QuestionAttempt();
			Question question2 = build2Question();
			questionAttempt2.setQuestion(question2);
			attempt.getQuestionAttempts().add(questionAttempt1);
			attempt.getQuestionAttempts().add(questionAttempt2);
			SessionUtil.putSessionAttribute(SessionAttr.QUIZ_ATTEMPT, attempt);
			this.question = attempt.getQuestionAttempts().get(0);
		}else{
			QuizAttempt attempt = (QuizAttempt) SessionUtil.getSessionAttribute(SessionAttr.QUIZ_ATTEMPT);
			this.question = attempt.getQuestionAttempts().get(0);
		}
	}
	
	@Override
	public QuestionAttempt getModel() {
		return question;
	}

}
